<template>
  <div>
    <el-card shadow="never" class="">
      <div class="mod-datasource">
        <el-form size="small" :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
          <el-form-item label="消息编号:">
            <el-input v-model.trim="dataForm.messageNum" placeholder="消息编号" clearable></el-input>
          </el-form-item>
          <el-form-item label="标题:">
            <el-input v-model.trim="dataForm.title" placeholder="标题" clearable></el-input>
          </el-form-item>
          <el-form-item label="发送时间:">
            <el-date-picker
                    v-model="createTimeRange"
                    type="daterange" :default-time="['00:00:00','23:59:59']"
                    format="yyyy-MM-dd HH:mm:ss"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期" clearable>
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button @click="queryReset()">重置</el-button>
            <el-button @click="getDataList()" type="primary">查询</el-button>
          </el-form-item>
        </el-form>

      </div>
    </el-card>
    <el-table
            size="medium"
            style="margin-top: 20px;"
            v-loading="dataListLoading"
            :header-cell-style="{background: '#DBEDF6'}"
            :data="dataList"
            @select="handleSelectionChange"
            :height="tableHeight">
      <el-table-column prop="messageNum" label="编号" header-align="center" align="center"></el-table-column>
      <el-table-column prop="title" label="标题" header-align="center" align="center"></el-table-column>
      <el-table-column prop="sendTime" label="发送时间" header-align="center" align="center"></el-table-column>
      <el-table-column prop="creator" label="发送人" header-align="center" align="center"></el-table-column>
      <el-table-column prop="receiveRead" label="状态" header-align="center" align="center">
        <template slot-scope="scope">
          {{scope.receiveRead === true ? '已读' : '未读'}}
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" header-align="center" align="center" width="150">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="getDetail(scope.row.id)">详情</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
            :current-page="page"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="limit"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="pageSizeChangeHandle"
            @current-change="pageCurrentChangeHandle">
    </el-pagination>
  </div>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import { formatDateTime } from '@/utils'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/message/messageInfo/page',
        getDataListIsPage: true,
        deleteURL: '/devtools/datasource',
        deleteIsBatch: true
      },
      dataForm: {
        messageNum: '',
        title: ''
      },
      tableHeight: null,
      messageDetailData: {},
      detailVisible: false,
      createTimeRange: '',
      deleteIds: []
    }
  },
  watch: {
    createTimeRange (newVal, oldVal) {
      if (newVal == null) {
        this.dataForm.startTime = null
        this.dataForm.endTime = null
      } else {
        this.dataForm.startTime = formatDateTime(newVal[0])
        this.dataForm.endTime = formatDateTime(newVal[1])
      }
    }
  },
  created () {
    this.$nextTick(() => {
      this.tableHeight = document.documentElement.clientHeight - 310
    })
  },
  methods: {
    getDetail (id) {
      this.$router.push('/messageManagement-detail/' + id)
    },
    queryReset () {
      this.createTimeRange = ''
      this.dataForm = {
        messageNum: '',
        title: '',
        startTime: '',
        endTime: ''
      }
    },
    deleteHandle (ids) {
      // const ids = [];
      // this.deleteIds.forEach((val) => {
      //   ids.push(val.id)
      // })
      this.$confirm('是否确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.dataListLoading = true
        this.$http.delete('/message/messageInfo', {
          data: [ids]
        }).then(res => {
          if (res.data.msg === 'success') {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.query()
          }
          this.dataListLoading = false
        })
      })
    },
    handleSelectionChange (val, el) {
      this.deleteIds = val
    }
  }
}
</script>
<style>
</style>
